package com.sgb.kjwl.view.ui.fragment.self;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.AtPackersManageBinding;
import com.sgb.kjwl.di.self.DaggerSelfFragComponent;
import com.sgb.kjwl.di.self.SelfFragModule;
import com.sgb.kjwl.model.entity.self.ResponseList;

import com.sgb.kjwl.model.entity.self.SelfTeamAchieveEntity;
import com.sgb.kjwl.model.entity.self.SelfTeamDeviceEntity;
import com.sgb.kjwl.model.entity.self.SelfTeamIntroduceEntity;
import com.sgb.kjwl.model.entity.self.SelfTeamMemberEntity;
import com.sgb.kjwl.model.entity.self.SelfTeamOfferEntity;
import com.sgb.kjwl.model.entity.self.SelfWorkerPublishEntity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfPackersManageActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfTeamAchieveEditActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfTeamDeviceActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfTeamDeviceEditActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfTeamIntroduceActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfTeamMemberEditActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfTeamOfferEditActivity;
import com.sgb.kjwl.view.ui.adapter.self.packers.SelfTeamAchieveAdapter;
import com.sgb.kjwl.view.ui.adapter.self.packers.SelfTeamDeviceAdapter;
import com.sgb.kjwl.view.ui.adapter.self.packers.SelfTeamMembersAdapter;
import com.sgb.kjwl.view.ui.adapter.self.packers.SelfTeamOfferAdapter;
import com.sgb.kjwl.viewmodel.self.packers.FragPackersManageModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.CommonDialog;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.util.MToast;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import static android.app.Activity.RESULT_OK;

public class FragPackersManageFragment extends AppLazyFragment {

    @Inject
    FragPackersManageModel viewModel;
    private AtPackersManageBinding binding;

    private int currentType = 1;
    private static final int REQUEST_ADD_CODE = 10021;

    private SelfTeamMembersAdapter membersAdapter;
    private SelfTeamOfferAdapter offerAdapter;
    private SelfTeamDeviceAdapter deviceAdapter;
    private SelfTeamAchieveAdapter achieveAdapter;

    public static FragPackersManageFragment newInstance(int type) {
        FragPackersManageFragment fragment = new FragPackersManageFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerSelfFragComponent.builder()
                .appComponent(appComponent)
                .selfFragModule(new SelfFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.at_packers_manage, container, false);
        return binding.getRoot();
    }

    private void initRequest(boolean isLoadMore) {
        switch (currentType) {
            case 1:
                viewModel.requestMemberList(isLoadMore);
                break;
            case 2:
                viewModel.requestOfferList(isLoadMore);
                break;
            case 3:
                viewModel.requestDeviceList(isLoadMore);
                break;
            case 4:
                viewModel.requestAchieveList(isLoadMore);
                break;
        }
    }

    @Override
    protected void initView() {
        currentType = getArguments().getInt("type", 1);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initRequest(false);
            }

            @Override
            public void onLoadMore() {
                initRequest(true);
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);

        binding.teamPublish.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.team_publish:
                viewModel.getTeamIntroduce();
                break;
        }
    }

    public void updateIntroduce(SelfTeamIntroduceEntity data) {
        if (data != null) {
            switch (currentType) {
                case 1:
                    Intent intent1 = new Intent(getActivity(), SelfTeamMemberEditActivity.class);
                    startActivityForResult(intent1, REQUEST_ADD_CODE);
                    break;
                case 2:
                    Intent intent2 = new Intent(getActivity(), SelfTeamOfferEditActivity.class);
                    startActivityForResult(intent2, REQUEST_ADD_CODE);
                    break;
                case 3:
                    Intent intent3 = new Intent(getActivity(), SelfTeamDeviceEditActivity.class);
                    startActivityForResult(intent3, REQUEST_ADD_CODE);
                    break;
                case 4:
                    Intent intent4 = new Intent(getActivity(), SelfTeamAchieveEditActivity.class);
                    startActivityForResult(intent4, REQUEST_ADD_CODE);
                    break;
            }
        }else {
            optTmpState();
        }
    }

    @Override
    protected void onLazyLoad() {
        initRequest(false);
    }

    public void showMemberList(ResponseList<List<SelfTeamMemberEntity>> list, boolean isLoadMore) {
        binding.layoutNullTeam.setVisibility(View.GONE);
        if (membersAdapter == null) {
            membersAdapter = new SelfTeamMembersAdapter(getActivity(), new ArrayList<>());
            binding.recyclerView.setAdapter(membersAdapter);
            binding.tvAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), SelfTeamMemberEditActivity.class);
                    startActivityForResult(intent, REQUEST_ADD_CODE);
                }
            });
            membersAdapter.setItemClick(new SelfTeamMembersAdapter.IRecyclerItemClick() {
                @Override
                public void delete(SelfTeamMemberEntity entity) {
                    optState(entity.getNo());
                }

                @Override
                public void edit(SelfTeamMemberEntity entity) {
                    Intent intent = new Intent(getActivity(), SelfTeamMemberEditActivity.class);
                    intent.putExtra(AppConstant.EXTRA_ENTITY, entity);
                    startActivityForResult(intent, REQUEST_ADD_CODE);
                }

                @Override
                public void onItemClick(SelfTeamMemberEntity entity) {
                    viewModel.getH5Url("teamMembersDetails", entity.getNo());
                }
            });
        }
        binding.linTopLabel.setVisibility(View.VISIBLE);
        binding.tvListTotal.setText("团队人员 " + list.getTotalRow() + " 人");
        binding.tvAdd.setText("添加团队人员");
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list.getList() != null && list.getList().size() > 0) {
                membersAdapter.addDatas(list.getList());
            }
            if (list.isLastPage()) {
                binding.recyclerView.setNoMore(true);
            }
        } else {
            binding.recyclerView.refreshComplete();
            if (list.getList() != null && list.getList().size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                membersAdapter.setDatas(list.getList());
            } else {
                membersAdapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
                binding.layoutNullTeam.setVisibility(View.VISIBLE);
                binding.tvNotData.setText("暂无团队人员");
                binding.teamPublish.setText("添加团队人员");
            }
        }
    }

    public void showOfferList(ResponseList<List<SelfTeamOfferEntity>> list, boolean isLoadMore) {
        binding.layoutNullTeam.setVisibility(View.GONE);
        if (offerAdapter == null) {
            offerAdapter = new SelfTeamOfferAdapter(getActivity(), new ArrayList<>());
            binding.recyclerView.setAdapter(offerAdapter);
            binding.tvAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), SelfTeamOfferEditActivity.class);
                    startActivityForResult(intent, REQUEST_ADD_CODE);
                }
            });
            offerAdapter.setItemClick(new SelfTeamOfferAdapter.IRecyclerItemClick() {
                @Override
                public void delete(SelfTeamOfferEntity entity) {
                    optState(entity.getNo());
                }

                @Override
                public void edit(SelfTeamOfferEntity entity) {
                    Intent intent = new Intent(getActivity(), SelfTeamOfferEditActivity.class);
                    intent.putExtra(AppConstant.EXTRA_ENTITY, entity);
                    startActivityForResult(intent, REQUEST_ADD_CODE);
                }

                @Override
                public void onItemClick(SelfTeamOfferEntity entity) {
                    viewModel.getOfferH5Url("workquotion", entity.getNo());
                }
            });
        }
        binding.linTopLabel.setVisibility(View.VISIBLE);
        binding.tvListTotal.setText("工种报价 " + list.getTotalRow() + " 项");
        binding.tvAdd.setText("添加工种报价");
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list.getList() != null && list.getList().size() > 0) {
                offerAdapter.addDatas(list.getList());
            }
            if (list.isLastPage()) {
                binding.recyclerView.setNoMore(true);
            }
        } else {
            binding.recyclerView.refreshComplete();
            if (list.getList() != null && list.getList().size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                offerAdapter.setDatas(list.getList());
            } else {
                offerAdapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
                binding.layoutNullTeam.setVisibility(View.VISIBLE);
                binding.tvNotData.setText("暂无报价");
                binding.teamPublish.setText("添加工种报价");
            }
        }
    }

    public void showDeviceList(ResponseList<List<SelfTeamDeviceEntity>> list, boolean isLoadMore) {
        binding.layoutNullTeam.setVisibility(View.GONE);
        if (deviceAdapter == null) {
            deviceAdapter = new SelfTeamDeviceAdapter(getActivity(), new ArrayList<>());
            binding.recyclerView.setAdapter(deviceAdapter);
            binding.tvAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), SelfTeamDeviceEditActivity.class);
                    startActivityForResult(intent, REQUEST_ADD_CODE);
                }
            });
            deviceAdapter.setItemCallBack(new SelfTeamDeviceAdapter.ItemCallBack() {
                @Override
                public void del(String id) {
                    optState(id);
                }

                @Override
                public void edit(String id) {
                    Intent intent = new Intent(getActivity(), SelfTeamDeviceEditActivity.class);
                    intent.putExtra(AppConstant.EXTRA_ENTITY, id);
                    startActivityForResult(intent, REQUEST_ADD_CODE);
                }
            });
            deviceAdapter.setOnItemClick(new AdapterOnItemClick<SelfTeamDeviceEntity>() {
                @Override
                public void onItemClick(SelfTeamDeviceEntity selfTeamDeviceEntity, int position) {
                    viewModel.getH5Url("groupEquipmentDeatil", selfTeamDeviceEntity.getNo());
                }
            });
        }
        binding.linTopLabel.setVisibility(View.VISIBLE);
        binding.tvListTotal.setText("专业设备 " + list.getTotalRow() + " 台");
        binding.tvAdd.setText("添加专业设备");
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list.getList() != null && list.getList().size() > 0) {
                deviceAdapter.addDatas(list.getList());
            }
            if (list.isLastPage()) {
                binding.recyclerView.setNoMore(true);
            }
        } else {
            binding.recyclerView.refreshComplete();
            if (list.getList() != null && list.getList().size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                deviceAdapter.setDatas(list.getList());
            } else {
                deviceAdapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
                binding.layoutNullTeam.setVisibility(View.VISIBLE);
                binding.tvNotData.setText("暂无设备");
                binding.teamPublish.setText("添加专业设备");
            }
        }
    }

    public void showAchieveList(ResponseList<List<SelfTeamAchieveEntity>> list, boolean isLoadMore) {
        binding.layoutNullTeam.setVisibility(View.GONE);
        if (achieveAdapter == null) {
            achieveAdapter = new SelfTeamAchieveAdapter(getActivity(), new ArrayList<>());
            binding.recyclerView.setAdapter(achieveAdapter);
            binding.tvAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), SelfTeamAchieveEditActivity.class);
                    startActivityForResult(intent, REQUEST_ADD_CODE);
                }
            });
            achieveAdapter.setItemClick(new SelfTeamAchieveAdapter.IRecyclerItemClick() {
                @Override
                public void delete(SelfTeamAchieveEntity entity) {
                    optState(entity.getNo());
                }

                @Override
                public void edit(SelfTeamAchieveEntity entity) {
                    Intent intent = new Intent(getActivity(), SelfTeamAchieveEditActivity.class);
                    intent.putExtra(AppConstant.EXTRA_ENTITY, entity);
                    startActivityForResult(intent, REQUEST_ADD_CODE);
                }

                @Override
                public void onItemClick(SelfTeamAchieveEntity entity) {
                    viewModel.getH5Url("constructionAchievementsDetails", entity.getNo());
                }
            });
        }
        binding.linTopLabel.setVisibility(View.VISIBLE);
        binding.tvListTotal.setText("工程业绩 " + list.getTotalRow() + " 项");
        binding.tvAdd.setText("添加工程业绩");
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list.getList() != null && list.getList().size() > 0) {
                achieveAdapter.addDatas(list.getList());
            }
            if (list.isLastPage()) {
                binding.recyclerView.setNoMore(true);
            }
        } else {
            binding.recyclerView.refreshComplete();
            if (list.getList() != null && list.getList().size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                achieveAdapter.setDatas(list.getList());
            } else {
                achieveAdapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
                binding.layoutNullTeam.setVisibility(View.VISIBLE);
                binding.tvNotData.setText("暂无业绩");
                binding.teamPublish.setText("添加工程业绩");
            }
        }
    }

    private void optState(String no) {
        new CommonDialog(getActivity()).builder().setPositiveButton("确定").setNegativeButton("取消")
                .setTitle("确认删除？")
                .setDialogCallBack(new CommonDialog.DialogCallBack() {
                    @Override
                    public void onRightClick() {
                        switch (currentType) {
                            case 1:
                                viewModel.deleteTeamMember(no);
                                break;
                            case 2:
                                viewModel.deleteTeamOffer(no);
                                break;
                            case 3:
                                viewModel.deleteTeamDevice(no);
                                break;
                            case 4:
                                viewModel.deleteTeamAchieve(no);
                                break;
                        }
                    }

                    @Override
                    public void onLeftClick() {

                    }
                }).show();
    }

    private void optTmpState() {
        new CommonDialog(getActivity()).builder().setPositiveButton("去完善").setNegativeButton("取消")
                .setTitle("请先完善工队信息")
                .setDialogCallBack(new CommonDialog.DialogCallBack() {
                    @Override
                    public void onRightClick() {
                        Intent intent = new Intent(getActivity(), SelfTeamIntroduceActivity.class);
                        startActivityForResult(intent, FragIntroduceFragment.REQUEST_EDIT_CODE);
                    }

                    @Override
                    public void onLeftClick() {

                    }
                }).show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_ADD_CODE) {
                initRequest(false);
            }
            if (requestCode == FragIntroduceFragment.REQUEST_EDIT_CODE){
                ((SelfPackersManageActivity) getActivity()).refreshTeamData();
            }
        }
    }
}
